<html>
<head><meta charset="utf-8"><title>efficiency Box&lt;[T; N]&gt; initilization ? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html">efficiency Box&lt;[T; N]&gt; initilization ?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224739496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224739496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224739496">(Feb 01 2021 at 15:13)</a>:</h4>
<p>Do we have a way on stable to efficiently init <code>Box&lt;[T; N]&gt;</code> that not through Vec ?</p>



<a name="224739726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224739726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224739726">(Feb 01 2021 at 15:15)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">a</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">vec!</span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">].</span><span class="n">into_boxed_slice</span><span class="p">().</span><span class="n">try_into</span><span class="p">().</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
</code></pre></div>
<p>generates bad code in debug mode.</p>



<a name="224739881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224739881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224739881">(Feb 01 2021 at 15:16)</a>:</h4>
<p>You could manually allocate, initialize that yourself, and then Box::from_raw</p>



<a name="224740013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224740013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224740013">(Feb 01 2021 at 15:17)</a>:</h4>
<p>but the unstable Box::new_uninit and Box::new_zeroed are the cleaner ways of doing that</p>



<a name="224741466"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224741466" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224741466">(Feb 01 2021 at 15:26)</a>:</h4>
<p>If you need to target stable, there are crates out there showcasing the functionality of the <code>unstable</code> API: <a href="https://docs.rs/uninit/0.4.0/uninit/extension_traits/trait.BoxUninit.html#optimized-in-place-heap-initialization">https://docs.rs/uninit/0.4.0/uninit/extension_traits/trait.BoxUninit.html#optimized-in-place-heap-initialization</a></p>



<a name="224743264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224743264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lzutao <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224743264">(Feb 01 2021 at 15:37)</a>:</h4>
<p>thanks for the suggestions.</p>
<p><span class="user-mention silent" data-user-id="243558">Steven Fackler</span> <a href="#narrow/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F/near/224740013">said</a>:</p>
<blockquote>
<p>but the unstable Box::new_uninit and Box::new_zeroed are the cleaner ways of doing that</p>
</blockquote>
<p>Those APIs are not bad. However, I don't see them convenient enough for <code>[T; N]</code>.<br>
But I cannot think about better API, I'll keep them in mind when they're in stable.</p>
<hr>
<p>Does </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="nb">Box</span>::<span class="n">uninit</span><span class="p">().</span><span class="n">init</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span><span class="w"></span>
</code></pre></div>
<p>generate code with alloc_zeros ?</p>



<a name="224743580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224743580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224743580">(Feb 01 2021 at 15:39)</a>:</h4>
<p>Not sure, but I'd really doubt it. That's why the zeroed constructor's there.</p>



<a name="224788253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224788253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224788253">(Feb 01 2021 at 20:46)</a>:</h4>
<p>Going through a slice is unlikely to work well since <code>Box&lt;[T]&gt;</code> will have to carry the length in its allocation while the array encodes it in the type and thus needs a box that's 1 usize smaller. Direct conversion methods obviously couldn't exist before const generics. Should they be added?</p>



<a name="224807048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/efficiency%20Box%3C%5BT%3B%20N%5D%3E%20initilization%20%3F/near/224807048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/efficiency.20Box.3C.5BT.3B.20N.5D.3E.20initilization.20.3F.html#224807048">(Feb 01 2021 at 23:10)</a>:</h4>
<p>Nevermind, I was wrong about the allocation.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>